SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		Emilio Augusto Veras Ivanovich
-- Create date: 03/12/2012
-- Description:	Insert or Update Complaint's table
-- =============================================
ALTER PROCEDURE [dbo].[PROCEDURE_COMPLAINTS]
	-- Add the parameters for the stored procedure here
	@ID BIGINT = 0,
	@TYPE_OF_SAMPLE_ID TINYINT, 
	@OPEN_BY_USER VARCHAR(60),
	@DOCTOR_ID VARCHAR(12),
	@DATE_OF_COLLECTION DATETIME,
	@COMMENTS VARCHAR(255) = NULL,
	@DEPARTAMENT VARCHAR(19),
	@CLOSE_BY_USER VARCHAR(60) = NULL
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	-- Insert statements for procedure here
	--Check whether exists or not the ID to be inserted.
	IF @ID = 0
	BEGIN
		-- Variables
		EXECUTE [dbo].[PROCEDURE_RESEED_COMPLAINTS];
		-- Insert statements for procedure here.
		INSERT INTO [SGLL].[dbo].[COMPLAINTS] ([TYPE_OF_SAMPLE_ID], [OPEN_BY_USER], [DOCTOR_ID], [DATE_OF_COLLECTION], [COMMENTS], [DEPARTAMENT], [CLOSE_BY_USER]) VALUES (@TYPE_OF_SAMPLE_ID, @OPEN_BY_USER, @DOCTOR_ID, @DATE_OF_COLLECTION, @COMMENTS, @DEPARTAMENT, @CLOSE_BY_USER);
	END;
	ELSE
	BEGIN
		-- Update statements for procedure here.
		UPDATE [SGLL].[dbo].[COMPLAINTS] SET [COMMENTS] = @COMMENTS, [CLOSE_BY_USER] = @CLOSE_BY_USER, [UPDATED] = GETDATE() WHERE [ID] = @ID;
	END;
END
GO
